<?php
/**
 * 基础库管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;

class Zhaoshangmgr extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companymgr');
    }

    /**
     * 项目列表
     */
    public function projectList(){
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');

        $page = max(1,intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0;
        if(!empty($keyword)){
            $where['title|title_en'] = array('like','%'.$keyword.'%');
        }

        $list = $this->model->listWherePage($where,'id desc',($page-1)*$pagesize,$pagesize);
        $total = $this->model->countWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total,'countpage'=>ceil($total/$pagesize)]]);
   
    }

    /**
     * 企业基本信息
     * @return \think\response\Json|void
     */
    public function companyDetail(){
       
        $id = $this->request->param('id');
        
        $where['id'] = $id;
        //$where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 新增项目
     * @return \think\response\Json|void
     */
    public function addProject(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $title_en = $this->request->param('title_en');
        $create_uid  = $this->admin['id'];
        $content = $this->request->param('content');
        $data = ['title'=>$title,'title_en'=>$title_en,'create_uid'=>$create_uid,'content'=>$content];

        $data['create_uid'] = $create_uid;
        $data['create_time'] = time();
        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function editProject(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $title_en = $this->request->param('title_en');
        $create_uid  = $this->admin['id'];
        $content = $this->request->param('content');
        $data = ['title'=>$title,'title_en'=>$title_en,'create_uid'=>$create_uid,'content'=>$content];

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除项目
     */
    public function delProject(){
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];

        $where = ['id'=>$id,'create_uid'=>$create_uid,'is_delete'=>0];
        $r = $this->model->countWhere($where);

        if(empty($r)){
            return json(['code'=>0,'msg'=>'记录不存在，请核实']);
        }

        $r = $this->model->deleteWhere(['id'=>$id]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

}